import React, {Component} from "react";
import {
	View,
	Text,
	PermissionsAndroid,
	Button,
} from "react-native";

async function requestCameraPermission() {
  PermissionsAndroid.requestMultiple([PermissionsAndroid.PERMISSIONS.CAMERA])
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CAMERA,
      {
        'title': '申请摄像头权限',
        'message': '一个很牛逼的应用想借用你的摄像头，' +
                   '然后你就可以拍出酷炫的皂片啦。'
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("现在你获得摄像头权限了")
    } else {
      console.log("用户并不屌你")
    }
  } catch (err) {
    console.warn(err)
  }
}

export default class PermissionsAndroidExam extends Component {
	constructor(props) {
		super(props);
	}
	onPress() {
		requestCameraPermission()
	}
	render() {
		return (
			<View>
				<Button 
					title="访问权限设置"
					onPress={() => requestCameraPermission()} 
				/>  
				<Text>权限提示</Text>
			</View>
		);
	}
}